package com.rui.shangXueTang.linked;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : DoublyLinkedList
 * @createTime : [2022/2/13 22:15]
 * @description : [双向链表]
 */

public class DoublyLinkedList {

    //头节点没有前驱节点
    private BookNode head = new BookNode(0,"",0.0);

    /**
     * 添加节点  在结尾添加
     */
    public void add(BookNode newNode){
        BookNode temp = head;

        while (true){
            if(temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = newNode;
        newNode.pre = temp;
    }

    /**
     * 修改节点  对比id修改
     */
    public boolean updateNode(BookNode newNode){
        BookNode temp = head;
        while (true){
            if(temp.next == null){
                return false;
            }
            if(temp.id == newNode.id){
                temp.name = newNode.name;
                temp.price = newNode.price;
                return true;
            }
            temp = temp.next;
        }
    }

    /**
     * 双向链表删除节点
     */
    public boolean deleteNode(int id){
        BookNode temp = head;
        while (true){
            if(temp.next == null){
                if(temp.id == id){
                    temp.pre.next = null;
                    return true;
                }else {
                    return false;
                }
            }
            if(temp.id == id){
                temp.pre.next = temp.next;
                temp.next.pre = temp.pre;
                return true;
            }
            temp = temp.next;
        }
    }

    /**
     * 查看双链表元素
     */
    public void list(){
        BookNode temp = head.next;
        while (temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
